package com.ns.scs.xf.utils;

import com.alibaba.fastjson2.JSON;
import com.ns.scs.xf.dto.XfDto;
import com.ns.scs.xf.model.*;
import okhttp3.WebSocket;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 发送请求内容
 *
 * @author lw
 */
public class XFSmsUtil {

    /**
     * 发送请求
     *
     * @param webSocket socket
     * @param histories 历史记录
     * @param listener  参数
     */
    public static void sendMsg(WebSocket webSocket, XfDto listener, List<Text> histories) {

        // header参数
        ReqHeader header = new ReqHeader();
        header.setApp_id(listener.getAppId());
        header.setUid(listener.getUserId());

        // 历史问题获取
        List<Text> text = new ArrayList<>();
        if (!histories.isEmpty()) {
            text.addAll(histories);
        }

        // 最新问题
        Text rc = new Text();
        rc.setRole(listener.getRole());
        rc.setContent(listener.getQuestion());
        text.add(rc);
        histories.add(rc);

        // text 参数
        ReqMsg message = new ReqMsg();
        message.setText(text);

        // payload参数
        ReqPayload payload = new ReqPayload();
        payload.setMessage(message);

        // parameter参数
        ReqBody requestJson = getRequestJson(listener, header, payload);
        webSocket.send(JSON.toJSONString(requestJson));
    }

    /**
     * 请求头
     *
     * @param listener 参数
     * @param header   请求头
     * @param payload  负载
     * @return ReqBody
     */
    private static @NotNull ReqBody getRequestJson(XfDto listener, ReqHeader header, ReqPayload payload) {
        Chat chat = new Chat();
        chat.setDomain(listener.getDomain());
        chat.setTemperature(listener.getTemperature());
        chat.setMax_tokens(listener.getMaxTokens());
        chat.setTop_k(listener.getTopK());
        chat.setShow_ref_label(listener.getShowRefLabel());
        Parameter parameter = new Parameter();
        parameter.setChat(chat);

        // 请求参数
        ReqBody requestJson = new ReqBody();
        requestJson.setHeader(header);
        requestJson.setParameter(parameter);
        requestJson.setPayload(payload);
        return requestJson;
    }
}
